using System;
using System.Data;
using System.Xml;

using OR.Reuse.Persistence;

using Derived = OR.Lazarus.Library.DataTier.Compiled;

namespace OR.Lazarus.Library.DataTier.Compiled.Lazarus
{
	/// <summary>
	/// Defines a set of objects that are returned as results of a method.
	/// </summary>
   public class ResultObjectRefDetail : EntityBase
   {
		protected System.String mName;
		protected string mRef;
		protected string mAlias;
		protected string mPKField;
		protected string mFKField;
		protected System.Boolean mRequired;
		protected string mDescription;
	
		protected Derived.ResultCollection mFields;

		public ResultObjectRefDetail()
		{
			mFields = new Derived.ResultCollection();
			mFields.ParentEntity = this;
	
		}
	
		/// <summary>
		/// 
		/// </summary>
		public System.String Name
		{
			get
			{
				return ( mName );
			}
			set
			{
				mName = value;
			}
		}

		/// <summary>
		/// 
		/// </summary>
		public string Ref
		{
			get
			{
				return ( mRef );
			}
			set
			{
				mRef = value;
			}
		}

		/// <summary>
		/// 
		/// </summary>
		public string Alias
		{
			get
			{
				return ( mAlias );
			}
			set
			{
				mAlias = value;
			}
		}

		/// <summary>
		/// 
		/// </summary>
		public string PKField
		{
			get
			{
				return ( mPKField );
			}
			set
			{
				mPKField = value;
			}
		}

		/// <summary>
		/// 
		/// </summary>
		public string FKField
		{
			get
			{
				return ( mFKField );
			}
			set
			{
				mFKField = value;
			}
		}

		/// <summary>
		/// 
		/// </summary>
		public System.Boolean Required
		{
			get
			{
				return ( mRequired );
			}
			set
			{
				mRequired = value;
			}
		}

		/// <summary>
		/// 
		/// </summary>
		public string Description
		{
			get
			{
				return ( mDescription );
			}
			set
			{
				mDescription = value;
			}
		}

		public Derived.ResultCollection Fields
		{
			get
			{
				return ( mFields );
			}
			set
			{
				mFields = value;
			}
		}

		public override void LoadXml( XmlElement pSource )
		{
			// Scalar Values
			mName = DataUtility.GetString( pSource, "name" );
			mRef = DataUtility.GetString( pSource, "ref" );
			mAlias = DataUtility.GetString( pSource, "alias" );
			mPKField = DataUtility.GetString( pSource, "pkfield" );
			mFKField = DataUtility.GetString( pSource, "fkfield" );
			mRequired = DataUtility.GetBool( pSource, "required", false );
			XmlElement vDescription = ( XmlElement )pSource.SelectSingleNode( "lzc:description", PersistenceConfig.Namespaces );
			
			if ( vDescription != null )
			{
				mDescription = vDescription.InnerText;
			}
			
			XmlElement vFields = ( XmlElement )pSource.SelectSingleNode( "lzc:fields", PersistenceConfig.Namespaces );
			
			if ( vFields != null )
			{
				mFields.LoadXml( vFields );
			}
		}
		
		public override XmlElement SaveXml( XmlDocument p_docContext )
		{
			XmlElement vNewElement	= p_docContext.CreateElement( "lzc", "object-ref", "http://schemas.icitadel.com/Lazarus/Compiled.xsd" );

			// Scalar Values
			if ( !String.IsNullOrEmpty( mName ) )
			{
				vNewElement.SetAttribute( "name", mName );
			}
					
			if ( !String.IsNullOrEmpty( mRef ) )
			{
				vNewElement.SetAttribute( "ref", mRef );
			}
					
			if ( !String.IsNullOrEmpty( mAlias ) )
			{
				vNewElement.SetAttribute( "alias", mAlias );
			}
					
			if ( !String.IsNullOrEmpty( mPKField ) )
			{
				vNewElement.SetAttribute( "pkfield", mPKField );
			}
					
			if ( !String.IsNullOrEmpty( mFKField ) )
			{
				vNewElement.SetAttribute( "fkfield", mFKField );
			}
					
			vNewElement.SetAttribute( "required", mRequired.ToString() );
			if ( !String.IsNullOrEmpty( mDescription ) )
			{
				XmlElement	vDescription = p_docContext.CreateElement( "lzc", "description", "http://schemas.icitadel.com/Lazarus/Compiled.xsd" );
				vDescription.InnerText = mDescription;
				vNewElement.AppendChild( vDescription );
			}
					

			// Collections
			
			XmlElement	vCollection	= null;
			
			vCollection = mFields.SaveXml( p_docContext );
			if ( vCollection != null )
			{
				vNewElement.AppendChild( vCollection );
			}

			return ( vNewElement );
		}

		public virtual object Clone()
		{
			Derived.ResultObjectRefDetail v_oReturn = new Derived.ResultObjectRefDetail();
			
			v_oReturn.Name = mName;
			v_oReturn.Ref = mRef;
			v_oReturn.Alias = mAlias;
			v_oReturn.PKField = mPKField;
			v_oReturn.FKField = mFKField;
			v_oReturn.Required = mRequired;
			v_oReturn.Description = mDescription;
			
			// FK Values

			// Collections

			foreach ( Derived.ResultDetail vDetail in mFields )
			{
				v_oReturn.Fields.Add( vDetail.CloneTyped() );
			}
			
			return ( v_oReturn );
		}

		public virtual Derived.ResultObjectRefDetail CloneTyped()
		{
			return ( ( Derived.ResultObjectRefDetail )this.Clone() );
		}
   }
}
   